Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.require([
             'Ext.grid.*',
             'Ext.data.*',
             'Ext.ux.grid.FiltersFeature',
             'Ext.toolbar.Paging',
             'Ext.ux.ajax.JsonSimlet',
             'Ext.ux.ajax.SimManager',
             'Ext.ux.grid.Printer'        
]);
Ext.define('TS.controller.RpAPaySheets',{
	extend: 'Ext.app.Controller',
	models:['rp.ContactBalanceModel','rp.ContactModel'],
	stores:['rp.APayDetailStore','rp.APaySummaryStore'],
	views:['rp.RpAPaySummarySheet','rp.RpAPayDetailSheet'],
	init:function() {
		this.control({
			'rpapaysummarysheet triggerfield[name=customer]':{specialkey:this.onSearchCust,change:this.onClearCust},
			'rpapaysummarysheet button[action=search]':{click:this.onSearchSummary},
			'rpapaysummarysheet button[action=advsearch]':{click:this.onAdvSearchSummary},
			'rpapaysummarysheet button[action=detail]':{click:this.onViewDetail},
			'rpapaysummarysheet button[action=printBill]':{click:this.onPrintBill},
			'rpapaysummarysheet button[action=exportBill]':{click:this.onExportBill},
			'rpapaysummarysheet grid':{itemdblclick:this.onDbClickBill},
			'rpapaydetailsheet triggerfield[name=customer]':{specialkey:this.onSearchCust,change:this.onClearCust},
			'rpapaydetailsheet button[action=search]':{click:this.onSearchDetail},
			'rpapaydetailsheet button[action=printBill]':{click:this.onPrintBill},
			'rpapaydetailsheet button[action=exportBill]':{click:this.onDetailExportBill}
		});
	},
	onSearchSummary:function(button) {
		var begYear = button.up('panel').down('numberfield[name=begYear]').getValue();
		var begPeriod = button.up('panel').down('numberfield[name=begPeriod]').getValue();
		var endYear = button.up('panel').down('numberfield[name=endYear]').getValue();
		var endPeriod = button.up('panel').down('numberfield[name=endPeriod]').getValue();
		var custId = button.up('panel').down('hiddenfield[name=custId]').getValue();
		var grid = button.up('form').up('panel').down('grid');
		var store = grid.getStore();
		store.on("beforeload",function(){
	        Ext.apply(grid.store.proxy.extraParams, {begYear:begYear,begPeriod:begPeriod,endYear:endYear,endPeriod:endPeriod,custId:custId});
	    });
		store.loadPage(1,{
			params:{
				begYear:begYear,
				begPeriod:begPeriod,
				endYear:endYear,
				endPeriod:endPeriod,
				custId:custId
			}
		});
	},
	onAdvSearchSummary:function(button) {
		
	},
	onSearchCust:function(field,e) {
		var custIdField = field.up('panel').down('hiddenfield[name=custId]');
		if(e.getKey() == e.ENTER && field.value != '') {
		    	var view=Ext.create('TS.view.base.CustomerSearchDialog');
		    	view.setVal(field.value);
		    	view.on({
				    selected: function(records){
				    	if(records.length == 0) {
				    		Ext.MessageBox.alert('ThinkGsp','未检索到任何客户信息!');
				    	} else {
					    	custIdField.setValue(records[0].get('customerId'));
					    	field.setValue('[' + records[0].get('customerCode') + ']' + records[0].get('customerName'));
				    	}
				    },
				    scope: this
				});
		    	view.show();	
	    }
	},
	onClearCust:function(field,e) {
		//var custIdField = field.up('panel').down('hiddenfield[name=custId]');
		if(field.value == '') {
			var custIdField = field.up('panel').down('hiddenfield[name=custId]');
			custIdField.setValue(null);
		}
	},
	onViewDetail:function(button) {
		var begYear = button.up('panel').up('panel').down('form').down('numberfield[name=begYear]').getValue();
		var begPeriod = button.up('panel').up('panel').down('form').down('numberfield[name=begPeriod]').getValue();
		var endYear = button.up('panel').up('panel').down('form').down('numberfield[name=endYear]').getValue();
		var endPeriod = button.up('panel').up('panel').down('form').down('numberfield[name=endPeriod]').getValue();
		var grid = button.up('grid');
		var custId = 0;
		var custName = '';
		if(grid.getSelectionModel().getSelection().length > 0) {
			custId = grid.getSelectionModel().getSelection()[0].get('custSuppId');
			custName = grid.getSelectionModel().getSelection()[0].getcustomer().get('customerName');
		}
		if(custId == 0) {
			Ext.Msg.show({
				title:'警告',
				msg:'您没有选择需要查看的客户!',
				buttons:Ext.Msg.OK,
				icon:Ext.Msg.WARNING
			});
			return;
		}
		return this.viewDetail(begYear,begPeriod,endYear,endPeriod,custId,custName);
	},
	onDbClickBill:function(view, record, item, index) {
		var begYear = view.up('panel').up('panel').down('form').down('numberfield[name=begYear]').getValue();
		var begPeriod = view.up('panel').up('panel').down('form').down('numberfield[name=begPeriod]').getValue();
		var endYear = view.up('panel').up('panel').down('form').down('numberfield[name=endYear]').getValue();
		var endPeriod = view.up('panel').up('panel').down('form').down('numberfield[name=endPeriod]').getValue();
		var custId = record.get('custSuppId');
		var custName = record.getcustomer().get('customerName');
		return this.viewDetail(begYear,begPeriod,endYear,endPeriod,custId,custName);
	},
	viewDetail:function(begYear,begPeriod,endYear,endPeriod,custId,custName) {
		if(custId == null || custId == 0) {
			Ext.Msg.show({
				title:'警告',
				msg:'您没选择要查询的客商',
				buttons:Ext.Msg.OK,
				icon:Ext.Msg.WARNING
			});
			return false;
		}
		var billForm = Ext.create('TS.view.rp.RpAPayDetailSheet',{
									closable : true,
									begYear:begYear,
									begPeriod:begPeriod,
									endYear:endYear,
									endPeriod:endPeriod,
									custId:custId,
									custName:custName
									});
		var main = Ext.getCmp("syscontent");
		var panel = main.add(billForm);
		main.setActiveTab(panel);
		var store = billForm.down('grid').getStore();
		store.load({
			params:{
				begYear:begYear,
				begPeriod:begPeriod,
				endYear:endYear,
				endPeriod:endPeriod,
				custId:custId
			}
		});
	},
	onPrintBill:function(button) {
		var grid = button.up('gridpanel');
		Ext.ux.grid.Printer.print(grid);
	},
	onExportBill:function(button) {
		var url="rp/apaylist/getAPaySummaryXLS.do?";
		var begYear = button.up('panel').up('panel').down('numberfield[name=begYear]').getValue();
		url += "begYear=" + begYear;
		var begPeriod = button.up('panel').up('panel').down('numberfield[name=begPeriod]').getValue();
		url += "&begPeriod=" + begPeriod;
		var endYear = button.up('panel').up('panel').down('numberfield[name=endYear]').getValue();
		url += "&endYear=" + endYear;
		var endPeriod = button.up('panel').up('panel').down('numberfield[name=endPeriod]').getValue();
		url += "&endPeriod=" + endPeriod;
		var custId = button.up('panel').up('panel').down('hiddenfield[name=custId]').getValue();
		if(custId=="")
			url += "&custId=0";
		else
			url += "&custId=" + custId;
		window.open(url);
	},
	onDetailExportBill:function(button) {
		var url="rp/apaylist/getAPayDetailXLS.do?";
		var begYear = button.up('panel').up('panel').down('numberfield[name=begYear]').getValue();
		url += "begYear=" + begYear;
		var begPeriod = button.up('panel').up('panel').down('numberfield[name=begPeriod]').getValue();
		url += "&begPeriod=" + begPeriod;
		var endYear = button.up('panel').up('panel').down('numberfield[name=endYear]').getValue();
		url += "&endYear=" + endYear;
		var endPeriod = button.up('panel').up('panel').down('numberfield[name=endPeriod]').getValue();
		url += "&endPeriod=" + endPeriod;
		var custId = button.up('panel').up('panel').down('hiddenfield[name=custId]').getValue();
		if(custId=="")
			url += "&custId=0";
		else
			url += "&custId=" + custId;
		window.open(url);
	},
	onSearchDetail:function(button) {
		var begYear = button.up('panel').down('numberfield[name=begYear]').getValue();
		var begPeriod = button.up('panel').down('numberfield[name=begPeriod]').getValue();
		var endYear = button.up('panel').down('numberfield[name=endYear]').getValue();
		var endPeriod = button.up('panel').down('numberfield[name=endPeriod]').getValue();
		var custId = button.up('panel').down('hiddenfield[name=custId]').getValue();
		var grid = button.up('form').up('panel').down('grid');
		var store = grid.getStore();
		store.load({
			params:{
				begYear:begYear,
				begPeriod:begPeriod,
				endYear:endYear,
				endPeriod:endPeriod,
				custId:custId
			}
		});
	}
});	